package org.example.day09;

/**
 * 第三章节3.2.5.1: 方法join(long) 和 sleep(long)的区别
 *      ①Thread.sleep()方法不会释放锁
 */
public class MyThread5 extends Thread{
    private Thread5B tb;

    public MyThread5(Thread5B tb) {
        this.tb = tb;
    }

    @Override
    public void run() {
        try {
            synchronized (tb){
                tb.start();
                Thread.sleep(6000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            Thread5B tb = new Thread5B();
            MyThread5 ta = new MyThread5(tb);
            ta.start();
            Thread.sleep(1000);
            Thread5C tc = new Thread5C(tb);
            tc.start();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Thread5B extends Thread{
    @Override
    public void run() {
        try {
            System.out.println("b run begin time = " + System.currentTimeMillis());
            Thread.sleep(5000);
            System.out.println("b run end time = " + System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    synchronized public void bService(){
        System.out.println("b currentTime = " + System.currentTimeMillis());
    }
}

class Thread5C extends Thread{
    private Thread5B tb;

    public Thread5C(Thread5B tb) {
        this.tb = tb;
    }

    @Override
    public void run() {
        tb.bService();
    }
}